import logging
import uuid

import pymysql
import base64
import datetime
from django.http import JsonResponse, StreamingHttpResponse, HttpResponse, Http404
from comment.comviews import com_views, success_response, err_response, format_response
from utils.dbutils import PymysqlUtil, DB_Config, MysqlTools
from utils.formatutils import get_params
from .sqlmap import *
from django.db import connection

mysql_tools = MysqlTools(db_config=DB_Config.get("default"))


@format_response
def index(request):
    active = request.GET.get("active")
    attach_id = request.GET.get("attach_id")
    if not attach_id:
        return err_response(210, "附件id不能为空")
    if active == "file_download_train":
        file_path = mysql_tools.select_one(FileSqlMap.TRAIN.format(**{"attach_id": attach_id}))
        dir = "/home/oasys/attachment" + file_path["file_path"]
        with open(dir, "rb") as f:
            img_data = f.read()
        return HttpResponse(img_data, content_type=file_path.get("content_type"))
    elif active == "file_download_other":
        file_path = mysql_tools.select_one(FileSqlMap.OTHER.format(**{"attach_id": attach_id}))
        dir = "/home/oasys/attachment" + file_path["attachment_path"]
        with open(dir, "rb") as f:
            img_data = f.read()
        return HttpResponse(img_data, content_type=file_path.get("attachment_type"))


# @format_response
# def index(request):
#     active = request.GET.get("active")
#     attach_id = request.GET.get("attach_id")
#     if not attach_id:
#         return Http404
#     if active == "file_download_train":
#         file_path = mysql_tools.select_one(FileSqlMap.TRAIN.format(**{"attach_id": attach_id}))
#         dir = "/home/work/oasys/file" + file_path["file_path"]
#         with open(dir, "rb") as f:
#             img_data = f.read()
#             try:
#                 return HttpResponse(img_data, content_type=file_path.get("content_type"))
#                 # response = HttpResponse(img_data)
#                 # response['content_type'] = file_path.get("content_type")
#                 # response['Content-Disposition'] = 'attachment; filename=' + file_path.get("file_name")
#                 # return response
#             except Exception:
#                 return Http404
#
#     elif active == "file_download_other":
#         file_path = mysql_tools.select_one(FileSqlMap.OTHER.format(**{"attach_id": attach_id}))
#         dir = "/home/work/oasys/file" + file_path["attachment_path"]
#         with open(dir, "rb") as f:
#             img_data = f.read()
#             try:
#                 return HttpResponse(img_data, content_type=file_path.get("attachment_type"))
#                 # response = HttpResponse(img_data)
#                 # response['content_type'] = file_path.get("attachment_type")
#                 # response['Content-Disposition'] = 'attachment; filename=' + file_path.get("attachment_name")
#                 # return response
#             except Exception:
#                 return Http404